#include <iostream>
#define MAX_VERTEX 26

using namespace std;

typedef struct
{
    int tail, wei;
} Node;
int main()
{
    int arcs[MAX_VERTEX][MAX_VERTEX] = {{0}};
    Node adjvex[MAX_VERTEX];
    int n, times=-1, output[100];
    while(cin>> n && n!=0)
    {
        ++times;
        for(int i=0; i<n-1; ++i)
        {
            char vex, cnext;
            int arcnum, next, wei;
            cin >> vex >> arcnum;
            for (int j=0; j<arcnum; ++j)
            {
                cin >> cnext >> wei;
                next = (int)cnext - 65;
                arcs[i][next] = wei;
                arcs[next][i] = wei;
            }
        }

        int start = 0, minimum = 0;//start from k, i.e. A
        adjvex[start].wei = 0;
        for(int i=0; i<n; ++i)
        {
            //Initialize
            if(i!=start)
            {
                adjvex[i].wei = arcs[start][i];
                adjvex[i].tail = start;
            }
        }


        for(int i=1; i<n; ++i)
        {
            //Find minimum vertex in adjvex[]
            int minvex = 0;
            while(adjvex[minvex].wei==0)
                ++minvex;//Find first !=0
            for(int j=minvex+1; j<n && adjvex[j].wei!=0; ++j)
            {
                if (adjvex[j].wei<adjvex[minvex].wei)
                    minvex = j;
            }

            //Add this minimum vertex
            minimum += arcs[adjvex[minvex].tail][minvex];
            for(int j=0; j<n; ++j)
            {
                if(arcs[minvex][j] << adjvex[j].wei)
                    adjvex[j].wei = arcs[minvex][j];
                    adjvex[j].tail = minvex;
            }
        }
        output[times] = minimum;
    }
    for (int i=0; i<=times; ++i) cout << output[i] << endl;
    return 0;
}


/*
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

216
30
*/
